<template>
  <Form ref="loginForm" :model="form" :rules="rules" @keydown.enter.native="handleSubmit">
    <FormItem prop="userName">
      <Input prefix="ios-contact" v-model="form.userName" placeholder="请输入用户名" size="large"/>
      <span slot="prepend">
        <Icon :size="16" type="ios-person"></Icon>
      </span>
    </FormItem>
    <FormItem prop="password">
      <Input
        prefix="ios-key"
        type="password"
        v-model="form.password"
        placeholder="请输入密码"
        size="large"
      />
      <span slot="prepend">
        <Icon :size="14" type="md-lock"></Icon>
      </span>
    </FormItem>
    <div style="margin-bottom:20px;">
      <Checkbox v-model="remebered">记住用户名和密码</Checkbox>
    </div>
    <FormItem>
      <Button @click="handleSubmit" size="large" type="primary" long>登录</Button>
    </FormItem>
  </Form>
</template>
<script>
import Secret from '../../libs/util/secret.js'
export default {
  mounted () {
    const username = localStorage.getItem('username')
    const pws = localStorage.getItem('pws')
    if (username && pws) {
      this.form.userName = Secret.Decrypt(username)
      this.form.password = Secret.Decrypt(pws)
    }
  },
  name: 'LoginForm',
  props: {
    userNameRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: '账号不能为空', trigger: 'blur' }]
      }
    },
    passwordRules: {
      type: Array,
      default: () => {
        return [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  data () {
    return {
      remebered: true,
      form: {
        userName: 'super_admin',
        password: ''
      }
    }
  },
  computed: {
    rules () {
      return {
        userName: this.userNameRules,
        password: this.passwordRules
      }
    }
  },
  methods: {
    handleSubmit () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$emit('on-success-valid', {
            userName: this.form.userName,
            password: this.form.password,
            remebered: this.remebered
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.ivu-form-item {
  margin-bottom: 30px;
}
</style>
